<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class attributes extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('attributes_model');
        }

        public function index(){show_404();}

        public function view_new($group_ID)
        {
            $this->data = array(
                'title' => "Add new attribute",
                'custom' => 1,
                'group_ID' => $group_ID
            );
            echo $this->load->view('attributes/new_attribute',$this->data,TRUE);
        }

        function view_edit($object_TYPE){

            $this->data = array(
                'title' => "Edit attributes",
                'type' => $object_TYPE,
                'object_attributes' => $this->atb_model->get_object_attributes($object_TYPE)
            );
            echo $this->load->view('attributes_to_object/edit_object_attributes',$this->data,TRUE);            
        }

        function view_all()
        {
            $this->data = array(
                'title' => "View attributes",
                'attributes' => $this->attributes_model->view_all_attributes()
            );
            $this->template->build('attributes/view_all_attributes',$this->data);
        }

        function submit_post()
        {

            if ($this->form_validation->run('attributes') == false)
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
            } 
            else
            {                            

                $record['custom'] = $_POST['newAttributeDialog_custom'];
                $record['value'] = $_POST['newAttributeDialog_value'];
                $record['name'] = $_POST['newAttributeDialog_name'];
                $record['required'] = $this->input->post('newAttributeDialog_required') ? 1 : 0;
                $record['groups_ID'] = $this->input->post('newAttributeDialog_group_ID');

                if ($this->attributes_model->insert($record) == TRUE)
                {
                    echo json_encode(array('action'=>true,'msg'=>'Successful.','id'=>$this->db->insert_id()));
                }
                else
                {
                    echo json_encode(array('action'=>FALSE, 'msg'=>'<p>An Error Occured.</p>')); 

                }
            }

        }

        function delete($ID){

            $this->attributes_model->delete_attributes_to_object($ID);

            if($this->attributes_model->delete_attribute($ID)>=0){
                $this->view_all();
            }else{
                echo 'Error ocured';
            }

        }

        function reorder_object_attributes()
        {
            $record['type'] = $_POST['editAttrToObjectDialog_type'];
            unset($_POST['editAttrToObjectDialog_type']);

            $cnt = 0;
            foreach ($_POST as $value):
                $id = explode('_',$value);
                $this->db->where('ID',$id[1]);
                $this->db->update('object_attributes',array('order'=>$cnt));
                $cnt++;
                endforeach;
        }

        function dropdown_check($str)
        {
            if ($str == '0')
            {
                $this->form_validation->set_message('dropdown_check', 'Please choose attribute <b>%s</b>!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }
}